import '../emojiOne/EmojiOne.dart';
import '../emojiOne/EmojiOneInfoes.dart';

class ImRichTextUtil {

  static const SPLIT = '￼';

  static String emojiToString(EmojiOne emojiOne) {
    return ImRichTextUtil.SPLIT + "e{" + emojiOne.getShortName() + "}";
  }

  static String emojisToString(List<Object> spans) {
    var result = '';
    for (var span in spans) {
      if (span is String) {
        result += span.toString();
      } else if (span is EmojiOne) {
        result += emojiToString(span);
      } else {
        throw Exception("不支持这种类型");
      }
    }
    return result;
  }

  static List<Object> splitEmojis(String content) {
    List<String> strList = split(content);
    var spans = <Object>[];
    for (var str in strList) {
      if (str.startsWith(ImRichTextUtil.SPLIT)) {
        var unicode = str.substring(3, str.length-1);
        var emoji = EmojiOneInfoes.getInstance().getByUnicodeOrShortName(unicode);
        if (emoji != null) {
          spans.add(emoji);
        }
      } else {
        spans.add(str);
      }
    }
    return spans;
  }
  
  static List<String> split(String content) {
    var result = <String>[];
    var length = content.length;
    var preIndex = 0;
    var isCheckSpan = false;
    for(var i=0; i<length; i++) {
      if (ImRichTextUtil.checkSpan(content, i)) {
        isCheckSpan = true;
        var preStr = content.substring(preIndex, i);
        if (preStr != '') {
          result.add(preStr);
        }
      if (i >0 ) {
        preIndex = i;
      }
    } else if (content[i] == '}' && isCheckSpan) {
      isCheckSpan = false;
      var span = content.substring(preIndex, i+1);
      result.add(span);
      preIndex = i+1;
      }
    }
    if (preIndex < length) {
      var mergeStr = content.substring(preIndex, length);
      if (mergeStr != "") result.add(mergeStr);
    }
    return result;
  }

  static bool checkSpan(String content, int index) {
    if (index+3 > content.length) return false;
    if (content[index] == ImRichTextUtil.SPLIT) {
      if (ImRichTextUtil.isLowerLetter(content[index+1])) {
        return content[index+2] == '{';
      }
    }
    return false;
  }

  static List<Object> getSelectSpan(List<Object> spans, int selectStart, int selectEnd) {
    var result = <Object>[];
    var size = 0;
    var selectStartIndex = -1;
    var selectEndIndex = -1;
    var startIndexFind = false;
    for(var span in spans) {
      if (!startIndexFind) selectStartIndex++;
      selectEndIndex++;
      if(span is String){
        var spanLength = span.length;
        if (size + spanLength < selectStart) {
          size += spanLength;
          continue;
        }

        startIndexFind = true;
        if (size + spanLength > selectEnd) {
          if (selectStartIndex == selectEndIndex) {
            var length = selectEnd - selectStart + 1;
            var start = selectStart - size;
            if (length > 0) {
              var str = span.substring(start, start + length);
              result.add(str);
            }
          } else {
            var length = selectEnd - size;
            if (length > 0) {
              var str = span.substring(0, length);
              result.add(str);
            }
          }

          break;
        } else {
          if (selectStartIndex == selectEndIndex) {
            var length = size + spanLength - selectStart;
            var start = selectStart - size;
            if (length > 0) {
              var str = span.substring(start, spanLength);
              result.add(str);
            }
          } else {
            result.add(span);
          }
          size += spanLength;
        }
      } else { // emoji
        var spanLength = 1;
        if (size + spanLength <= selectStart) {
          size += spanLength;
          continue;
        }

        startIndexFind = true;
        if (size + spanLength >= selectEnd) {
          result.add(span);
          break;
        } else {
          size += spanLength;
          result.add(span);
        }
      }
    }
    return result;
  }

  // 判断是否为字母
  static bool isUpperLetter(String val) {
    return RegExp(r'^[A-Z]+$').hasMatch(val);
  }

  static bool isLowerLetter(String val) {
    return RegExp(r'^[a-z]+$').hasMatch(val);
  }

  // 判断是否为中文
  static bool isChinese(String val) {
    return RegExp(r'^[\u4e00-\u9fa5]+$').hasMatch(val);
  }

  // 判断是否为数字
  static bool isNumber(String val) {
    return RegExp(r'^[0-9]+$').hasMatch(val);
  }
}